package com.sctu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.sctu.EMSApp.BaseActivity;
import com.sctu.EMSApp.R;
import com.sctu.customView.CustomViewPager;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhu on 2015/11/3.
 */
public class ScenicFragment extends Fragment implements View.OnClickListener{
    private RelativeLayout introduction, sight, round, surround;
    private TextView introductionText, sightText, roundText, surroundText;
    private Fragment introductionFragment, sightFragment, roundFragment, surroundFragment;
    private CustomViewPager viewPager;
    private List<Fragment> fragmentList;
    private  View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        if (view == null){
            view = inflater.inflate(R.layout.fragment_scenictour, container, false);
            initView();
            initClick();
            initFragment();
            deal();
        }

        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }


        return view;
    }

    private void initView() {
        introduction = (RelativeLayout) view.findViewById(R.id.scenictour_introduction_top);
        sight = (RelativeLayout) view.findViewById(R.id.scenictour_sight_top);
        round = (RelativeLayout) view.findViewById(R.id.scenictour_round_top);
        surround = (RelativeLayout) view.findViewById(R.id.scenictour_surround_top);

        introductionText = (TextView) view.findViewById(R.id.scenictour_introduction_top_text);
        sightText = (TextView) view.findViewById(R.id.scenictour_sight_top_text);
        roundText = (TextView) view.findViewById(R.id.scenictour_round_top_text);
        surroundText = (TextView) view.findViewById(R.id.scenictour_surround_top_text);

        viewPager = (CustomViewPager) view.findViewById(R.id.scenictour_viewpager);
    }

    private void initClick() {
        introduction.setOnClickListener(this);
        sight.setOnClickListener(this);
        round.setOnClickListener(this);
        surround.setOnClickListener(this);

    }

    private void initFragment() {
        introductionFragment = new ScenictourIntroduceFragment();
        sightFragment = new ScenictourSightFragment();
        roundFragment = new ScenictourRoundFragment();
        surroundFragment = new ScenictourSurroundFragment();

        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(introductionFragment);
        fragmentList.add(sightFragment);
        fragmentList.add(roundFragment);
        fragmentList.add(surroundFragment);
    }

    private void deal() {

        viewPager.setAdapter(new MyFrageStatePagerAdapter(getActivity().getSupportFragmentManager()));
        setDefault();
    }

    private void setDefault(){

        changeBack(introduction);
        changeText(introductionText);
        viewPager.setCurrentItem(0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scenictour_introduction_top:
                changeBack(introduction);
                changeText(introductionText);
                viewPager.setCurrentItem(0);
                break;
            case R.id.scenictour_sight_top:
                changeBack(sight);
                changeText(sightText);
                viewPager.setCurrentItem(1);
                break;
            case R.id.scenictour_round_top:
                changeBack(round);
                changeText(roundText);
                viewPager.setCurrentItem(2);
                break;
            case R.id.scenictour_surround_top:
                changeBack(surround);
                changeText(surroundText);
                viewPager.setCurrentItem(3);
                break;
        }
    }

    private void changeBack(RelativeLayout view){
        introduction.setBackgroundResource(R.drawable.top_background);
        sight.setBackgroundResource(R.drawable.top_background);
        round.setBackgroundResource(R.drawable.top_background);
        surround.setBackgroundResource(R.drawable.top_background);

        view.setBackgroundResource(R.drawable.top_background_on);

    }

    private void changeText(TextView view){

        introductionText.setTextColor(getResources().getColor(R.color.green));
        sightText.setTextColor(getResources().getColor(R.color.green));
        roundText.setTextColor(getResources().getColor(R.color.green));
        surroundText.setTextColor(getResources().getColor(R.color.green));

        view.setTextColor(getResources().getColor(R.color.white));
    }


    private class MyFrageStatePagerAdapter extends FragmentStatePagerAdapter {

        public MyFrageStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }
        private boolean isCanScroll = true;

        @Override
        public Fragment getItem(int i) {

            return fragmentList.get(i);
//            return null;
        }


            @Override
        public int getCount() {
            return fragmentList.size();
//            return 0;
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
//            switch (viewPager.getCurrentItem()){
//                case 0:
//                    changeBack(introduction);
//                    changeText(introductionText);
//                    break;
//                case 1:
//                    changeBack(sight);
//                    changeText(sightText);
//                    break;
//                case 2:
//                    changeBack(round);
//                    changeText(roundText);
//                    break;
//                case 3:
//                    changeBack(surround);
//                    changeText(surroundText);
//                    break;
//            }
        }

    }

    @Override

    public void onDetach() {

        super.onDetach();

        try {

            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");

            childFragmentManager.setAccessible(true);

            childFragmentManager.set(this, null);


        } catch (NoSuchFieldException e) {

            throw new RuntimeException(e);

        } catch (IllegalAccessException e) {

            throw new RuntimeException(e);

        }

    }



    }
